from machine import Pin
import utime
import network
import ntptime 
import time
from machine import RTC
from machine import Pin, I2C
import machine
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)  
rtc = RTC()
pin1 = Pin(12, Pin.OUT)
pin2 = Pin(13, Pin.OUT)
pin3 = Pin(22, Pin.OUT)
pin1.on()
pin2.off()
pin3.off()
def sync_ntp():
    global sta_if
    # ntptime.NTP_DELTA = 3155644800 # UTC+8 的 magic number
    ntptime.NTP_DELTA = 3155673600
    ntptime.host = 'ntp1.aliyun.com'
    for _ in  range(10):
        try:
            ntptime.settime()
        except:
            time.sleep(1)
            print(f"try ntptime {_} ")
            continue
        else:
            print(rtc.datetime())
            sta_if.active(False) 
            return True
    return False



#sta_if.connect("<AP_name>", "<password>") # Connect to an AP
def connectWifi():
    global sta_if
    sta_if.active(True) 
    n=0
    if not sta_if.isconnected():
        sta_if.connect("Network_1", "15368274761")
        # sta_if.connect("ChinaNet-5Wi4","twvey5ag")
        while not sta_if.isconnected():
            n=n+1
            print("connect...%02d"%(n))
            utime.sleep(1)
            if(n>=30):
                sta_if.active(False)
                return False
        print("connected")
def f1():

    pin3.off()
    connectWifi()
    n=0
    sync_ntp()
    while True:
        pin3.on()
        

        n=n+1
        if (n>3600):
            connectWifi()
            sync_ntp()
             
            print(f"sync_ntp {n}")
            n=0
        t=rtc.datetime()
        print("{0:0>2d}:{1:0>2d}:{2:0>2d}\t\t{3:0>4d}".format(t[4],t[5],t[6],n))
        print(t)
        t=rtc.datetime()
        # print(int(t[7]/1000))
        utime.sleep_ms(20)
        pin3.on()
        machine.lightsleep(1000-int(t[7]/1000))  
        # utime.sleep_us(1000000-t[7])
        # if(n>10):
        #     n=0
        #     machine.deepsleep(5000)
def f():
    print("1")
    pin3.off()
    # connectWifi()
    # n=0
    # sync_ntp()
    pin3.on()
def deepwake():
    while True:
        t=rtc.datetime()
        print(time.mktime(t))
        print("{0:0>2d}:{1:0>2d}:{2:0>2d}\t\t{3:0>3d}".format(t[4],t[5],t[6]))
        t=rtc.datetime()
        utime.sleep_us(1000000-t[7])

if __name__ == "__main__":
    from time import mktime, gmtime
    from machine import RTC
    import ntptime
    import PFC8563
    # try:
    #     ntptime.host = "sk.pool.ntp.org"
    #     ntptime.timeout = 5
    #     teraz = ntptime.time()
    # except:
    #     pass
    # 
    PFC8563.connectWifi()
    rtc=machine.RTC()
    t=rtc.datetime()
    print(t)
    nt=ntptime.time()
    print(nt)
    tm = utime.gmtime(nt+3600*8)
    print(tm)
    # PFC8563.sync()
    # rok = gmtime(teraz)[0]
    # letny = list(gmtime(mktime((rok, 3, 31, 1, 0, 0, 0, 0))))
    # letny[2] -= (letny[6] + 1) % 7
    # letny = mktime(letny)
    # zimny = list(gmtime(mktime((rok, 10, 31, 1, 0, 0, 0, 0))))
    # zimny[2] -= (zimny[6] + 1) % 7
    # zimny = mktime(zimny)
    # if (letny < teraz and teraz < zimny):
    #     teraz += 7200
    # else:
    #     teraz += 3600

    # t = gmtime(teraz)
    # RTC().datetime((t[0], t[1], t[2], None, t[3], t[4], t[5], 1))
